package com.cwvs.controller.admin;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cwvs.services.AppDepartmentService;
import com.cwvs.services.AppStoreService;
import com.cwvs.utils.JsonUtil;
import com.cwvs.utils.Pages;

@Controller
@RequestMapping("/admin")
public class AdminAppDepartmentController {

	@Resource
	private AppDepartmentService appDepartmentService;
	@Resource
	private AppStoreService appStoreService;
	@RequestMapping("/department_view")
	public ModelAndView departmentView(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_department/app_department_list");
		return mv;
	}
	@RequestMapping("/department_list")
	public @ResponseBody
	Map<String, Object> departmentList(HttpServletRequest request,
			HttpServletResponse response) {
		Pages page = new Pages();
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		page.setPageSize(Integer.parseInt(limit));
		page.setCurrentPage(Integer.parseInt(start) / Integer.parseInt(limit)
				+ 1);
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("NAME", request.getParameter("name"));
		Pages rePage = appDepartmentService.departmentList(paramsMap, page);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", rePage.getPage());
		ret.put("totalCount", rePage.getTotalRecord());
		return ret;
	}
	/**
	 * 科室保存
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/department_save")
	public @ResponseBody
	Map<String, Object> departmentSave(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String,Object> map=new HashMap<String,Object>();
		Enumeration paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					map.put(paramName.toUpperCase(), paramValue);
				}
			}
		}
		Map<String,Object> res=new HashMap<String,Object>();
		boolean b=appDepartmentService.saveOrUpdate(map);
		if(b){
			res.put("message", "保存成功");
		}else{
			res.put("errormsg", "保存失败");
		}
		res.put("success", b);
		return res;
	}
	@RequestMapping("/department_edit")
	public ModelAndView departmentEdit(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_department/app_department_edit");
		String id=request.getParameter("id");
		mv.addObject("stores", JsonUtil.toJson(appStoreService.storeList(null, null, null, null, "2", null)));
		if(StringUtils.isNotBlank(id)){
			mv.addObject("model",appDepartmentService.getDepartmentInfo(id));
		}
		return mv;
	}
	/**
	 * 科室停用
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/department_false")
	public @ResponseBody
	Map<String, Object> departmentFalse(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		boolean b=appDepartmentService.departmentSetStaus(id, false);
		if(b){
			res.put("message", "处理成功");
		}else{
			res.put("errormsg", "处理失败");
		}
		res.put("success", b);
		return res;
	}
	
	/**
	 * 科室启用
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/department_true")
	public @ResponseBody
	Map<String, Object> departmentTrue(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		boolean b=appDepartmentService.departmentSetStaus(id, true);
		if(b){
			res.put("message", "处理成功");
		}else{
			res.put("errormsg", "处理失败");
		}
		res.put("success", b);
		return res;
	}
}
